<?php

/**
 * VanillaPost form.
 *
 * @package    vanilla
 * @subpackage form
 * @author     Emiliano Nobis <emiliano.nobis@gmail.com>
 * @version    SVN: $Id: sfPropelFormTemplate.php 10377 2008-07-21 07:10:32Z dwhittle $
 */
class VanillaPostForm extends BaseVanillaPostForm
{
  public function configure()
  {
      unset($this['created_at']);

      $this->embedI18n(array('it', 'en'));
      $this->widgetSchema->setLabel('it', 'Italiano');
      $this->widgetSchema->setLabel('en', 'Inglese');

      $this->widgetSchema['preview'] = new sfWidgetFormInputFileEditable(array(
        'file_src' => '../../../uploads/blog/thumb/'.$this->getObject()->getPreview(),
        'is_image' => true,
        'edit_mode' => !$this->isNew(),
        'template'  => '<div>%file% %delete% elimina<br/><br/>%input%</div>'
      ));

      $this->validatorSchema['preview'] = new sfValidatorFile(array(
            'required' => false,
            'path' => sfConfig::get('sf_upload_dir').'/blog',
            'mime_types' => 'web_images'
       ));

      $this->validatorSchema['preview_delete'] = new sfValidatorPass();

      $this->widgetSchema['date'] = new sfWidgetFormJQueryDate(array('culture' => 'it', 'format' => '%day%%month%%year%'));
  }

  protected function processUploadedFile($field, $filename = null, $values = null) {
      $fn = parent::processUploadedFile($field, $filename, $values);

      if($fn != null) {
          $thumb = array('dir' => 'thumb', 'width' => '200', 'height' => '200');

          $thumbnail = new sfthumbnail($thumb['width'], $thumb['height'], true, false);
          $thumbnail->loadFile(sfConfig::get('sf_upload_dir')."blog".$fn);
          $thumbnail->save(sfConfig::get('sf_upload_dir')."blog/".$thumb['dir']."/".$fn, 'image/jpeg');
      }

      return $fn;
  }
}
